package com.lwp.demo.js;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class TestMain {

    public static void main(String[] args) {
        JSONObject obj = new JSONObject();
        obj.put("a123", "val1");
        obj.put("c11", "val2");
        obj.put("b123", "val3");
        obj.put("b22", "val4");



        System.out.println(obj.toJSONString()); // 一行输出
        System.out.println(JSON.toJSONString(obj, true));
        System.out.println(JSON.toJSONString(obj, SerializerFeature.PrettyFormat, SerializerFeature.SortField));

        List<String> strList = new ArrayList<>();
        strList.add("123");
        strList.add("222");
        System.out.println(JSON.toJSONString(strList));

        List<A> aList = new ArrayList<>();
        aList.add(new A("01", 1));
        aList.add(new A("02", 2));
        aList.add(new A("01", 3));
        aList.add(new A("03", 4));
        System.out.println(JSON.toJSONString(aList));

        // 过滤
        List<A> collect = aList.stream().filter(a -> a.getStatus().equals("01")).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(collect));
        // 集合转
        List<String> collect1 = aList.stream().filter(a -> a.getStatus().equals("01")).map(a -> a.getStatus()).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(collect1));

        // 集合转
        List<Integer> collect2 = aList.stream().filter(a -> a.getStatus().equals("01")).map(a -> a.getId()).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(collect2));

    }
}

class A{
    private String status;
    private int id;

    public A(String status, int id) {
        this.status = status;
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}